package me.w70.bot.wrappers;

import java.awt.Point;
import java.awt.Rectangle;

import me.w70.bot.Bot;
import me.w70.bot.methods.Calc;
import me.w70.hooks.GameObject;
import me.w70.hooks.GroundTile;
import me.w70.hooks.InteractivePhysicalObject;
import me.w70.hooks.SpecialWorldObject;
import me.w70.hooks.Tile;
import me.w70.hooks.WorldObject;

/**
 * Created by IntelliJ IDEA. User: Jan Ove / Kosaki Date: 08.mai.2009 Time:
 * 17:14:07
 */
public class RSObject extends PhysicalObject {

	public RSObject(me.w70.hooks.PhysicalObject physObj) {
		super(physObj);
	}

	@Override
	public int getID() {
		if (physObj instanceof InteractivePhysicalObject) {
			final GameObject gameObject = ((InteractivePhysicalObject) physObj)
					.getGameObject();
			if (gameObject instanceof SpecialWorldObject) {
				return ((SpecialWorldObject) gameObject).getID();
			}
		}
		return super.getID(); // To change body of overridden methods use File
								// | Settings | File Templates.
	}

	public InteractivePhysicalObject getInteractivePhysicalObject() {
		if (physObj instanceof InteractivePhysicalObject) {
			return (InteractivePhysicalObject) physObj;
		}
		return null;
	}

	public boolean doActionAtDoor(char direction, String action) {
		direction = java.lang.Character.toLowerCase(direction);
		if (Bot.methods.getClient().isMenuOpen()) {
			Bot.methods.atMenu("Cancel");
		}
		Tile objectPos = getLocation();
		Point t = null;
		if (direction == 'n') {
			t = Calc.tileToScreen(objectPos.getX(), objectPos.getY() + 1, .5,
					.1, 40);
		} else if (direction == 's') {
			t = Calc.tileToScreen(objectPos.getX(), objectPos.getY(), .5, .1,
					40);
		} else if (direction == 'w') {
			t = Calc.tileToScreen(objectPos.getX(), objectPos.getY(), .1, .5,
					40);
		} else if (direction == 'e') {
			t = Calc.tileToScreen(objectPos.getX() + 1, objectPos.getY(), .1,
					.5, 40);
		}
		if (t == null) {
			return false;
		}
		Bot.methods.moveMouse(t, 3, 3);
		return Bot.methods.atMenu(action);

	}

	@Override
	public Point getScreenPos() {
		Rectangle bounds = getBounds();
		if (bounds.width == 0 || bounds.height == 0)
			return super.getScreenPos();
		return Calc.tileToScreen(bounds.x, bounds.y,
				((double) bounds.width) / 2, ((double) bounds.height) / 2,
				-getHeight() / 2);

	}

	public int getHeight() {
		if (physObj instanceof InteractivePhysicalObject) {
			GameObject gameObject = ((InteractivePhysicalObject) physObj)
					.getGameObject();
			if (gameObject instanceof WorldObject) {
				return ((WorldObject) gameObject).getHeight();
			}
		}
		return -10;
	}

	public Rectangle getBounds() {
		try {
			int size = 4;
			final Tile loc = getLocation();
			int minX = loc.getX();
			int maxX = loc.getX() + size;
			int minY = loc.getY();
			int maxY = loc.getY() + size;

			int ID = getID();

			int x = minX;
			int y = minY;
			int width = 0;
			int height = 0;
			mainLoop: for (; x <= maxX; x++) {
				GroundTile tile = Bot.methods.getClient().getGroundTiles()[Bot.methods
						.getClient().getPlane()][x
						- Bot.methods.getClient().getBaseX()][y
						- Bot.methods.getClient().getBaseY()];
				if (tile == null)
					break;
				me.w70.hooks.PhysicalObject physicalObject = tile
						.getBoundaryPhysicalObject();
				if (physicalObject != null) {
					if (physicalObject.getUID() == UID) {
						width++;
						continue;
					}
				}
				physicalObject = tile.getPhysicalObject2();
				if (physicalObject != null) {
					if (physicalObject.getUID() == UID) {
						width++;
						continue;
					}
				}
				physicalObject = tile.getPhysicalObject3();
				if (physicalObject != null) {
					if (physicalObject.getUID() == UID) {
						width++;
						continue;
					}
				}
				physicalObject = tile.getItemPhysicalObject();
				if (physicalObject != null) {
					if (physicalObject.getUID() == UID) {
						width++;
						continue;
					}
				}

				me.w70.hooks.PhysicalObject[] physicalObjects = tile
						.getInteractivePhysicalObjects();
				if (physicalObjects != null && physicalObjects.length != 0) {
					for (me.w70.hooks.PhysicalObject physicalObject1 : physicalObjects) {
						if (physicalObject1 != null) {
							if (physicalObject1.getUID() == UID) {
								width++;
								continue mainLoop;
							}
						}
					}
				}
			}
			x = minX;
			mainLoop: for (; y <= maxY; y++) {
				GroundTile tile = Bot.methods.getClient().getGroundTiles()[Bot.methods
						.getClient().getPlane()][x
						- Bot.methods.getClient().getBaseX()][y
						- Bot.methods.getClient().getBaseY()];
				if (tile == null)
					break;
				me.w70.hooks.PhysicalObject physicalObject = tile
						.getBoundaryPhysicalObject();
				if (physicalObject != null) {
					if (physicalObject.getUID() == UID) {
						height++;
						continue;
					}
				}
				physicalObject = tile.getPhysicalObject2();
				if (physicalObject != null) {
					if (physicalObject.getUID() == UID) {
						height++;
						continue;
					}
				}
				physicalObject = tile.getPhysicalObject3();
				if (physicalObject != null) {
					if (physicalObject.getUID() == UID) {
						height++;
						continue;
					}
				}
				physicalObject = tile.getItemPhysicalObject();
				if (physicalObject != null) {
					if (physicalObject.getUID() == UID) {
						height++;
						continue;
					}
				}

				me.w70.hooks.PhysicalObject[] physicalObjects = tile
						.getInteractivePhysicalObjects();
				if (physicalObjects != null && physicalObjects.length != 0) {
					for (me.w70.hooks.PhysicalObject physicalObject1 : physicalObjects) {
						if (physicalObject1 != null) {
							if (physicalObject1.getUID() == UID) {
								height++;
								continue mainLoop;
							}
						}
					}
				}
			}
			return new Rectangle(minX, minY, width, height);
		} catch (IndexOutOfBoundsException ignored) {
			return new Rectangle(0, 0, 0, 0);
		}
	}

}
